package com.fly.estore.dao.Impl;

import com.fly.estore.dao.BaseDAO;
import com.fly.estore.dao.ModuleDAO;
import com.fly.estore.domain.ModuleInfo;
import org.springframework.jdbc.core.PreparedStatementSetter;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Fly on 2016/4/13.
 */
@Repository("ModuleDAO")
public class ModuleDAOImpl extends BaseDAO<ModuleInfo> implements ModuleDAO {

    public int add(final ModuleInfo moduleInfo) {
        int update = 0;
        StringBuilder sql = new StringBuilder();
        sql.append("insert into module_info(module_id, module_name, module_area, module_href, parent_module_id, module_icon, order_num, show_menu, level)")
                .append("values(?, ?, ?, ?, ?, ?, ?, ?, ?)");
        update = getJdbcTemplate().update(sql.toString(), new PreparedStatementSetter() {
            public void setValues(PreparedStatement ps) throws SQLException {
                int i = 1;
                ps.setString(i++, moduleInfo.getModuleId());
                ps.setString(i++, moduleInfo.getModuleName());
                ps.setString(i++, moduleInfo.getModuleArea());
                ps.setString(i++, moduleInfo.getModuleHref());
                ps.setString(i++, moduleInfo.getParentModule().getModuleId());
                ps.setString(i++, moduleInfo.getModuleIcon());
                ps.setInt(i++, moduleInfo.getOrderNum());
                ps.setString(i++, moduleInfo.getShowMenu());
                ps.setInt(i++, moduleInfo.getLevel());

            }
        });
        return update;
    }

    public int update(final ModuleInfo moduleInfo) {
        int update = 0;
        StringBuilder sql = new StringBuilder();
        sql.append("update module_info set")
                .append(" module_name = ?,")
                .append(" module_area = ?,")
                .append(" module_href = ?,")
                .append(" parent_module_id = ?,")
                .append(" module_icon = ?,")
                .append(" order_num = ?,")
                .append(" show_menu = ?,")
                .append(" level = ?")
                .append(" where module_id = ?");
        update = getJdbcTemplate().update(sql.toString(), new PreparedStatementSetter() {
            public void setValues(PreparedStatement ps) throws SQLException {
                int i = 1;
                ps.setString(i++, moduleInfo.getModuleName());
                ps.setString(i++, moduleInfo.getModuleArea());
                ps.setString(i++, moduleInfo.getModuleHref());
                ps.setString(i++, moduleInfo.getParentModule().getModuleId());
                ps.setString(i++, moduleInfo.getModuleIcon());
                ps.setInt(i++, moduleInfo.getOrderNum());
                ps.setString(i++, moduleInfo.getShowMenu());
                ps.setInt(i++, moduleInfo.getLevel());
                ps.setString(i++, moduleInfo.getModuleId());
            }
        });
        return update;
    }

    public int deleteById(final String moduleId) {
        int update = 0;
        StringBuilder sql = new StringBuilder();
        sql.append("delete from module_info where module_id = ?");
        update = getJdbcTemplate().update(sql.toString(), new PreparedStatementSetter() {
            public void setValues(PreparedStatement ps) throws SQLException {
                ps.setString(1, moduleId);
            }
        });
        return update;
    }

    public ModuleInfo getById(String moduleId) {
        ModuleInfo moduleInfo = null;
        StringBuilder sql = new StringBuilder();
        sql.append("select * from module_info where module_id = ?");
        List<ModuleInfo> resultList = getJdbcTemplate().query(sql.toString(), new ModuleRowMapper(), new Object[]{moduleId});
        if (!CollectionUtils.isEmpty(resultList)){
            moduleInfo = resultList.get(0);
        }
        return moduleInfo;
    }

    public List<ModuleInfo> getListByParentId(String parentId) {
        List<ModuleInfo> moduleInfoList = new ArrayList<ModuleInfo>();
        StringBuilder sql = new StringBuilder();
        sql.append("select * from module_info where parent_module_id = ? order by order_num");
        moduleInfoList = getJdbcTemplate().query(sql.toString(), new ModuleRowMapper(), new Object[]{parentId});
        return moduleInfoList;
    }

    public class ModuleRowMapper implements RowMapper<ModuleInfo>{
        public ModuleInfo mapRow(ResultSet rs, int rowNum) throws SQLException {
            ModuleInfo moduleInfo = new ModuleInfo();
            moduleInfo.setModuleId(rs.getString("module_id"));
            moduleInfo.setModuleName(rs.getString("module_name"));
            moduleInfo.setModuleArea(rs.getString("module_area"));
            moduleInfo.setModuleHref(rs.getString("module_href"));
            ModuleInfo parentModule = new ModuleInfo();
            parentModule.setModuleId(rs.getString("parent_module_id"));
            moduleInfo.setParentModule(parentModule);
            moduleInfo.setModuleIcon(rs.getString("module_icon"));
            moduleInfo.setOrderNum(rs.getInt("order_num"));
            moduleInfo.setShowMenu(rs.getString("show_menu"));
            moduleInfo.setLevel(rs.getInt("level"));
            return moduleInfo;
        }
    }
}
